package com.webserver.controllers;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.User;

import javax.jws.soap.SOAPBinding;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class UserController {
    private static String usersDir="./users/";
    static {
        File userDir =new File(usersDir);
        if (!userDir.exists()){
            userDir.mkdirs();
        }
    }
    public void reg(HttpRequest request, HttpResponse response){
        System.out.println("开始处理用户注册！！！！");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String nickname=request.getParameter("nickname");
        String ageStr =request.getParameter("age");
        int age =Integer.parseInt(ageStr);
        System.out.println(username+","+password+","+nickname+","+age);
        try(FileOutputStream fos =new FileOutputStream(
                usersDir+username+".obj");
            ObjectOutputStream oos =new ObjectOutputStream(fos);
        ){
            User user =new User(username,password,nickname,age);
            oos.writeObject(user);
            File file =new File("./webapps/myweb/reg_success.html");
            response.setEntity(file);
        }catch (IOException e){
            e.printStackTrace();
        }

    }
    public void login(HttpRequest request,HttpResponse response){
        System.out.println("开始处理登录！！！！！");
        String username= request.getParameter("username");
        String password =request.getParameter("password");
       File file =new File("./webapps/myweb/login_fail.html");
       if (username==null || password==null){
           response.setEntity(file);
           return;
       }
       File userFile =new File(usersDir+username+".obj");
       if (userFile.exists()){
           try (ObjectInputStream ois= new ObjectInputStream(new FileInputStream(userFile));){
               User user =(User)ois.readObject();
               if (user.getPassword().equals(password)){
                  file =new File("./webapps/myweb/login_success.html");
                   response.setEntity(file);
                   return;
               }
           }catch (IOException | ClassNotFoundException e){
               e.printStackTrace();
           }
       }
        response.setEntity(file);
    }
    public void showAllUser(HttpRequest request,HttpResponse response){
        System.out.println("开始处理显示所有用户！！！");
        List<User> userList =new ArrayList<>();
        File dir =new File(usersDir);
        File[] users =dir.listFiles(f->f.getName().endsWith(".obj"));
        for (File userFile : users){
            try (ObjectInputStream ois =new ObjectInputStream(new FileInputStream(userFile))
            ){
                User user =(User)ois.readObject();
                userList.add(user);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        System.out.println(userList);

            PrintWriter pw =response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>用户列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>用户列表</h1>");
            pw.println("<table border=\"1\">");
            pw.println("<tr>");
            pw.println("<td>用户名</td>");
            pw.println("<td>密码</td>");
            pw.println("<td>昵称</td>");
            pw.println("<td>年龄</td>");
            pw.println("</tr>");
            for (User user: userList){
                pw.println("<tr>");
                pw.println("<td>"+user.getUsername()+"</td>");
                pw.println("<td>"+user.getPassword()+"</td>");
                pw.println("<td>"+user.getNickname()+"</td>");
                pw.println("<td>"+user.getAge()+"</td>");
                pw.println("</tr>");
            }
            pw.println("</table>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");
            response.setContentType("text/html");
    }
}
